﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iMKelly {
    using CoreClass;
    public delegate void CompleHandler();
    public partial class frmCreateUser : Form {
        public event CompleHandler Complete;
        private bool isCreateMode = true;
        private Database dbs;
        public frmCreateUser() {
            InitializeComponent();
        }
        public frmCreateUser(Database dbs) {
            InitializeComponent();
            this.dbs = dbs;
            type.SelectedIndex = 0;
            id.Text = dbs.getNextUserID(Database.UserType.Reader);
        }

        public frmCreateUser(Database dbs, UserInfo info) {
            InitializeComponent();
            this.dbs = dbs;
            type.SelectedIndex = 0;
            id.Text = info.UserID;
            name.Text = info.Name; ssn.Text = info.SSN; addr.Text = info.Address; date.Text = info.BirthDate;
            if (info.Sex == true) sex.SelectedIndex = 0; else sex.SelectedIndex = 1;
            isCreateMode = false;
        }

        private void OK_Click(object sender, EventArgs e) {
            UserInfo info = Generate();
            if (isCreateMode) {
                dbs.createNewUser(info);
            }
            else {
                dbs.updateUserInfo(id.Text, info);
            }
            Complete();
            this.Dispose();
        }

        private UserInfo Generate() {
            bool s = sex.SelectedIndex == 0 ? true : false;
            return new UserInfo(id.Text, name.Text, ssn.Text, addr.Text, date.Text, s, 7);
        }

        private void button2_Click(object sender, EventArgs e) {
            this.Dispose();
        }

    }
}
